package com.hazz.kotlinmvp.utils

import android.content.Context
import android.util.DisplayMetrics
import android.graphics.PointF
import android.support.v7.widget.LinearSmoothScroller
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.LinearLayoutManager



/**
 * 创建日期：2018/3/13
 *
 * 描述:
 *
 * Created by admin.
 *
 * gitHub: https://github.com/KungFuteddy
 *
 * Orporate Name: Henan Huimeng future network technology Co. Ltd.
 */
class FastScrollManger : LinearLayoutManager {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout) {}

    override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, position: Int) {
        val linearSmoothScroller = object : LinearSmoothScroller(recyclerView.context) {

            override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
                return this@FastScrollManger.computeScrollVectorForPosition(targetPosition)
            }

            //控制速度。
            override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
                return super.calculateSpeedPerPixel(displayMetrics)
            }

            override fun calculateTimeForScrolling(dx: Int): Int {
                var dx = dx
                if (dx > 3000) {
                    dx = 3000
                }

                return super.calculateTimeForScrolling(dx)
            }
        }

        linearSmoothScroller.targetPosition = position
        startSmoothScroll(linearSmoothScroller)
    }
}